package com.yc.video.controller;

import com.alibaba.fastjson.JSONObject;
import com.yc.video.model.TSUser;
import com.yc.video.support.utils.AjaxJson;
import com.yc.video.support.utils.DateUtil;
import com.yc.video.support.utils.video.FFmpegImgUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
//
//	@Autowired
//	private UserServiceImpl userService;

	@RequestMapping(value = "/registOrLogin", method = RequestMethod.POST)
	public Object registOrLogin(@RequestBody TSUser user) {
		return null;
	}

	/**
	 * 获取帧
	 * i
	 *
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upload")
	public Object upload(@RequestParam("file") MultipartFile file) {
		Integer number = Integer.valueOf(request.getParameter("number"));//第几帧
		String codeMap = request.getParameter("codeMap");//第几帧
		try {
			Map<String, String> map = JSONObject.parseObject(codeMap, Map.class);
			if (file.isEmpty()) {
				return AjaxJson.errorMsg("上传失败，请选择文件");
			}
			String fileCode = file.getContentType();
			String fileName = map.get(fileCode);
			File fileTwo = new File("文件临时传输");
			FileUtils.copyInputStreamToFile(file.getInputStream(), fileTwo);
			if (number == null || number < 0) number = 5;

			String filePath = FFmpegImgUtil.getdesktopPath + File.separator + DateUtil.getDay();
			File filedir = new File(filePath);
			//如果文件夹不存在则创建
			if (!filedir.exists() && !filedir.isDirectory()) filedir.mkdirs();
			FFmpegImgUtil.videoImage(fileTwo, number, filePath, 0,
					fileName != null ? fileName.substring(0, fileName.lastIndexOf(".")) : "帧图");
			return AjaxJson.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxJson.errorMsg("上传失败！请重试");
		}

	}


}
